/**
 * 
 */
package com.tech.freak.pyp.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author Julian Suarez
 *
 */
public class PicoyPlacaDatabasehelper extends SQLiteOpenHelper {
	
	private static final String TAG = "PicoyPlacaDatabasehelper";
	
	private static PicoyPlacaDatabasehelper instance=null;
	
	private DatabaseUtils.InsertHelper	mPyPInserter;

	public PicoyPlacaDatabasehelper(Context context) {
		super(context, PyPContentProvider.DATABASE_NAME, null, PyPContentProvider.DATABASE_VERSION);
	}

	/* (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		final String[] create_table_statements = 
			{ 	
				"CREATE TABLE IF NOT EXISTS "+ PicoyPlaca.PYP_TABLE_NAME+ " ("
				+PicoyPlaca.PYP._ID+ " INTEGER , " 
				+PicoyPlaca.PYP.CITY+" , "
				+PicoyPlaca.PYP.MONDAY+" , "
				+PicoyPlaca.PYP.TUESDAY+" , "
				+PicoyPlaca.PYP.WEDNSEDAY+" , "
				+PicoyPlaca.PYP.THURSDAY+" , "
				+PicoyPlaca.PYP.FRIDAY+" , "
				+PicoyPlaca.PYP.SATURDAY+" , "
				+PicoyPlaca.PYP.SUNDAY+" , "
				+"PRIMARY KEY("+PicoyPlaca.PYP._ID+") )"
			};

		for (int i = 0 ; i < create_table_statements.length; i++)
			db.execSQL(create_table_statements[i]);
	}

	/* (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        
        final String[] tables = {PicoyPlaca.PYP_TABLE_NAME};
        
        for (int i = 0; i < tables.length; i++)
        	db.execSQL("DROP TABLE IF EXISTS "+tables[i]);
        
        onCreate(db);

	}
	
	public static synchronized PicoyPlacaDatabasehelper getInstance(Context context){
		if(instance == null){
			instance = new PicoyPlacaDatabasehelper(context);
		}
		return instance;
	}

	/* (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onOpen(android.database.sqlite.SQLiteDatabase)
	 */
	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
		
		mPyPInserter = new DatabaseUtils.InsertHelper(db, PicoyPlaca.PYP_TABLE_NAME);

	}

	public long pypInsert(ContentValues values){
		
		return mPyPInserter.insert(values);
		
	}
}
